Subclass QXmlStreamWriter. Use it to abstract the test for null strings
authorrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Thu, 21 Feb 2013 21:53:20 +0000 (21:53 +0000)
committerrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Thu, 21 Feb 2013 21:53:20 +0000 (21:53 +0000)
(which is different for C Strings and QStrings) to avoid writing empty
XML tags.

gpsbabel/gpx.cc
gpsbabel/src/core/xmlstreamwriter.h [new file with mode: 0644]

index 6b905f1ce09c3aab7a18627beb3142eb559cde14..0369ac22f07a3b766891e57db328d8822221cb46 100644 (file)
@@ -1,8 +1,7 @@
 /*
     Access GPX data files.
 
-    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008,
-      2009, 2010 Robert Lipe, gpsbabel.org
+    Copyright (C) 2002-2013 Robert Lipe, gpsbabel.org
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -28,7 +27,7 @@
 #include <expat.h>
 static XML_Parser psr;
 #endif
-#include <QtCore/QXmlStreamWriter>
+#include "src/core/xmlstreamwriter.h"
 #include <QtCore/QRegExp>
 #include <QtCore/QDebug>
 //#include <QtCore/QTextCodec>
@@ -57,7 +56,7 @@ static gbfile* fd;
 static const char* input_fname;
 static gbfile* ofd = NULL;
 static QString ostring;
-static QXmlStreamWriter writer(&ostring);
+static gpsbabel::XmlStreamWriter writer(ostring);
 static short_handle mkshort_handle;
 static const char* link_url;
 static char* link_text;
@@ -1667,12 +1666,8 @@ write_gpx_url(const waypoint* waypointp)
     url_link* tail;
     for (tail = (url_link*)&waypointp->url_next; tail; tail = tail->url_next) {
       writer.writeStartElement("link");
-      if(tail->url && tail->url[0]) {
         writer.writeAttribute("href", tail->url);
-      }
-      if(tail->url_link_text && tail->url_link_text[0]) {
-        writer.writeTextElement("text", tail->url_link_text);
-      }
+        writer.writeOptionalTextElement("text", tail->url_link_text);
       // FIXME This is to force empty links to not be self-closing.  This is
       // lame, but it's for compatibilty with our old writer to minimize thrash
       // on the Qt transition.
@@ -1682,9 +1677,7 @@ write_gpx_url(const waypoint* waypointp)
     return;
   }
   writer.writeTextElement("url", QString(urlbase) + QString(waypointp->url));
-  if (waypointp->url_link_text && waypointp->url_link_text[0]) {
-    writer.writeTextElement("urlname", QString(waypointp->url_link_text));
-  }
+  writer.writeOptionalTextElement("urlname", QString(waypointp->url_link_text));
 #endif
 }
 
@@ -1871,15 +1864,11 @@ gpx_write_common_description(const waypoint* waypointp, const char* indent,
   if (oname) {
     writer.writeTextElement("name", oname);
   }
-  if (waypointp->description && waypointp->description[0]) {
-    writer.writeTextElement("cmt", waypointp->description);
-  }
+  writer.writeOptionalTextElement("cmt", waypointp->description);
   if (waypointp->notes && waypointp->notes[0]) {
     writer.writeTextElement("desc", waypointp->notes);
   } else {
-    if (waypointp->description && waypointp->description[0]) {
-      writer.writeTextElement("desc", waypointp->description);
-    }
+    writer.writeOptionalTextElement("desc", waypointp->description);
   }
   write_gpx_url(waypointp);
   if (!waypointp->icon_descr.isNull()) {
@@ -1950,12 +1939,8 @@ gpx_track_hdr(const route_head* rte)
   }
 #else
   writer.writeStartElement("trk");
-  if (rte->rte_name && rte->rte_name[0]) {
-    writer.writeTextElement("name", rte->rte_name);
-  }
-  if (rte->rte_desc && rte->rte_desc[0]) {
-    writer.writeTextElement("desc", rte->rte_desc);
-  }
+  writer.writeOptionalTextElement("name", rte->rte_name);
+  writer.writeOptionalTextElement("desc", rte->rte_desc);
   if (rte->rte_num) {
     writer.writeTextElement("number", QString::number(rte->rte_num));
   }
@@ -2090,12 +2075,8 @@ gpx_route_hdr(const route_head* rte)
   write_optional_xml_entity(ofd, "  ", "name", rte->rte_name);
   write_optional_xml_entity(ofd, "  ", "desc", rte->rte_desc);
 #else
-  if (rte->rte_name && rte->rte_name[0]) {
-    writer.writeTextElement("name", rte->rte_name);
-  }
-  if (rte->rte_desc && rte->rte_desc[0]) {
-    writer.writeTextElement("desc", rte->rte_desc);
-  }
+  writer.writeOptionalTextElement("name", rte->rte_name);
+  writer.writeOptionalTextElement("desc", rte->rte_desc);
 #endif
   if (rte->rte_num) {
 #if OLDGPX
diff --git a/gpsbabel/src/core/xmlstreamwriter.h b/gpsbabel/src/core/xmlstreamwriter.h
new file mode 100644 (file)
index 0000000..1d7668e
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+    Copyright (C) 2013 Robert Lipe, gpsbabel.org
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
+
+ */
+
+
+#include <QtCore/QXmlStreamWriter>
+
+// As this code began in C, we have several hundred places that write
+// c strings.  Add a test that the string contains anything useful
+// before serializing an empty tag.
+
+namespace gpsbabel {
+
+class XmlStreamWriter : public QXmlStreamWriter {
+public:
+  XmlStreamWriter(QString& s) : QXmlStreamWriter(&s) {}
+
+  // Dont emit the attribute if there's nothing interesting in it.
+  void writeOptionalAttribute(QString tag, QString value) {
+    if (!value.isEmpty()) {
+      writeAttribute(tag, value);
+    }
+  }
+
+  // Dont emit the tag if there's nothing interesting in it.
+  void writeOptionalTextElement(QString tag, QString value) {
+    if (!value.isEmpty()) {
+      writeTextElement(tag, value);
+    }
+  }
+
+};
+
+}; // namespace gpsbabel